ALTER TABLE Promotion
ADD PromixityRadiusFixedCost MONEY NULL,
	PromixityRadiusRatePerPush MONEY NULL,
	CostTimingFixedCost MONEY NULL,
	CostTimingRatePerPush MONEY NULL
GO
ALTER PROCEDURE [dbo].[spAPI_Promotion_ProximityRadius_Update]
(
	@Token VARCHAR(255),
	@PromotionId INT,
	
	@ProximityRadiusId INT,
	@CustomRange INT = NULL,
	@Over200MilesFlag BIT,
	@RegisteredOutsideOfZip BIT,
	@RegisteredOutsideOfState BIT,
	@RegisteredOutsideOfCity BIT
)
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @IdResult INT
	DECLARE @BusinessId INT
	
	DECLARE @FixedCost MONEY, @RatePerPush MONEY
		
	SET @IdResult = 0
	EXEC spAPI_Business_CheckToken @Token = @Token, @Result = @IdResult OUT, @BusinessId = @BusinessId OUT
	
	IF @IdResult = 1
		IF EXISTS(SELECT 1 FROM Promotion WHERE BusinessId = @BusinessId AND PromotionId = @PromotionId)
		BEGIN
			SELECT @FixedCost = FixedCost, @RatePerPush = RatePerPush
			FROM ProximityRadius
			WHERE ProximityRadiusId = @ProximityRadiusId
			
			UPDATE Promotion
			SET ProximityRadiusId = @ProximityRadiusId, CustomRange = @CustomRange, 
				Over200MilesFlag = @Over200MilesFlag, 
				RegisteredOutsideOfZip = @RegisteredOutsideOfZip, 
				RegisteredOutsideOfState = @RegisteredOutsideOfState, 
				RegisteredOutsideOfCity = @RegisteredOutsideOfCity,
				PromixityRadiusFixedCost = @FixedCost, 
				PromixityRadiusRatePerPush = @RatePerPush
			WHERE (PromotionId = @PromotionId)
		END	
		ELSE
			SET @IdResult = -1

	SELECT @IdResult AS IdResult
END
GO
ALTER PROCEDURE [dbo].[spAPI_Promotion_BaseCostTiming_Update]
(
	@Token VARCHAR(255),
	@PromotionId INT,
	
	@BaseCostTimingId INT,
	@BaseCostTimingScheduledAt DATETIME = NULL,
	@BaseCostTimingStarts DATETIME = NULL,
	@BaseCostTimingEnds DATETIME = NULL
)
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @IdResult INT
	DECLARE @BusinessId INT
	
	DECLARE @FixedCost MONEY, @RatePerPush MONEY
			
	SET @IdResult = 0
	EXEC spAPI_Business_CheckToken @Token = @Token, @Result = @IdResult OUT, @BusinessId = @BusinessId OUT
	
	IF @IdResult = 1
		IF EXISTS(SELECT 1 FROM Promotion WHERE BusinessId = @BusinessId AND PromotionId = @PromotionId)
		BEGIN
			SELECT @FixedCost = FixedCost, @RatePerPush = RatePerPush
			FROM BaseCostTiming
			WHERE BaseCostTimingId = @BaseCostTimingId
			
			UPDATE Promotion
			SET BaseCostTimingId = @BaseCostTimingId, BaseCostTimingScheduledAt = @BaseCostTimingScheduledAt, 
				BaseCostTimingStarts = @BaseCostTimingStarts, BaseCostTimingEnds = @BaseCostTimingEnds,
				CostTimingFixedCost = @FixedCost, CostTimingRatePerPush = @RatePerPush
			WHERE (PromotionId = @PromotionId)
		END	
		ELSE
			SET @IdResult = -1

	SELECT @IdResult AS IdResult
END
GO